﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TC.WEB.Site.Core;
using TC.WEB.Site.Models;
using TC.WEB.Site.Core.Model;
using TC.SSO.Server.Filters;

namespace TC.SSO.Server.Controllers
{
    public static class NameValueCollectionHelper
    {
        public static System.Collections.Specialized.NameValueCollection ParseQueryString()
        {
            var context = HttpContext.Current;
            if (context == null)
            {
                return null;
            }
            var rult = HttpUtility.ParseQueryString(GetContentFromStream(context.Request.InputStream));
            return rult;
        }

        public static object ParseObject()
        {
            var context = HttpContext.Current;
            if (context == null)
            {
                return null;
            }
            var rult = new System.Web.Script.Serialization.JavaScriptSerializer().DeserializeObject(GetContentFromStream(context.Request.InputStream));
            return rult;
        }

        public static T ParseTObject<T>()
        {
            var context = HttpContext.Current;
            if (context == null)
            {
                return default(T);
            }
            var rult = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<T>(GetContentFromStream(context.Request.InputStream));
            return rult;
        }

        public static string GetContent()
        {
            var context = HttpContext.Current;
            if (context == null)
            {
                return string.Empty;
            }
            return GetContentFromStream(context.Request.InputStream);
        }

        public static string GetContentFromStream(System.IO.Stream stream)
        {
            using (var sr = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8))
            {
                string content = sr.ReadToEnd();
                return content;
            }
        }
    }
    /// <summary>
    /// 单点登录的操作方法
    /// </summary>
    [RequestExceptionTokenFilter][RequestTokenFilter]
    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    public class TokenController : Controller
    {
        static UserAccountTokenModel _m;
        static TokenController()
        {
            _m = BaseModel.M<UserAccountTokenModel>();
        }

        /// <summary>
        /// 根据令牌获取用户信息
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        public ActionResult GetUser(string token)
        {
            var rult = _m.GetUserInfo(token);
            return Json(rult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据令牌获取用户账户信息
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        public ActionResult GetUserAccount(string token)
        {
            var rult = _m.GetUserAccountInfo(token);
            return Json(rult,JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 刷新Token信息
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        public ActionResult Refresh(string token)
        {
            var rult = _m.RefreshToken(token);
            return Json(rult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取Token状态
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        public ActionResult GetTokenState(string token)
        {
            var rult = _m.GetTokenState(token);
            return Json(rult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 签名检验
        /// </summary>
        /// <returns></returns>
        public ActionResult Verify()
        {
            return Json(new OperRult { Success = true }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetLast(int uid, int last)
        {
            var list = MessageCenter.Current.Get(last, uid, 10, 20);
            if (list.Count() == 0)
            {
                return Json(new { Success = true, Value = new { Last = last, Value = new string[0] } }, JsonRequestBehavior.AllowGet);
            }
            last = list.Max(obj => obj.Id);
            return Json(new { Success = true, Value = new { Last = last, Value = list.Select(obj => obj.Message).ToArray() } }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SetMessage()
        {

            Message msg = NameValueCollectionHelper.ParseTObject<Message>();
            MessageCenter.Current.Set(msg);
            return Json(new { Success = true, Value = 0 }, JsonRequestBehavior.AllowGet);
        }
    }
}
